package org.anrc.poi;

import android.app.Activity;
import android.app.Notification;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.location.Location;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class PoiRadarService extends Service {
	SharedPreferences prefs;
	Cursor poiCursor;
	private int myAlertRadiusValue;
	private MediaPlayer mMediaPlayer;
	Notification notification;

	@Override
	public void onStart(Intent intent, int startId) {
		getPreferences();
		runAlert();

	}

	private void getPreferences() {
		prefs = getSharedPreferences(MainPreferences.USER_PREFERENCES,
				Activity.MODE_PRIVATE);
		int alertRadiusIndex = prefs.getInt(MainPreferences.PREF_ALERT_RADIUS,
				2);

		if (alertRadiusIndex < 0)
			alertRadiusIndex = 2;
		Resources r = getResources();
		int[] alertRadiusValues = r.getIntArray(R.array.alert_radius_values);

		myAlertRadiusValue = alertRadiusValues[alertRadiusIndex];

	}

	private void runAlert() {
		Thread runAlertThread = new Thread(null, backgroundRunAlert,
				"refresh_poi");
		runAlertThread.start();
	}

	private Runnable backgroundRunAlert = new Runnable() {

		public void run() {
			try {
				doRunAlert();
			} catch (Exception e) {
				Log.e("SERVECE", "Blad przy wykonywaniu refresh_poi "
						+ e.getMessage());
			}
		}

		private void doRunAlert() {
			Uri poiURI = PoiProvider.CONTENT_URI;
			ContentResolver cr = getContentResolver();

			// TODO to jakos trzeba uzaleznic od promienia
			// ale jeszcze nie wiem jak
			double tolerance = 0.1;
			Location curLocation = PoiRadar.myLocation;
			Location destLocation = PoiRadar.myLocation;

			double minLat = curLocation.getLatitude() - tolerance;
			double maxLat = curLocation.getLatitude() + tolerance;

			double minLng = curLocation.getLongitude() - tolerance;
			double maxLng = curLocation.getLongitude() + tolerance;

			String selection = PoiProvider.KEY_LOCATION_LAT + " between "
					+ minLat + " and " + maxLat + " and "
					+ PoiProvider.KEY_LOCATION_LNG + " between " + minLng
					+ " and " + maxLng;

			poiCursor = cr.query(poiURI, null, selection, null, null);

			int i = 0;
			if (poiCursor.moveToFirst())
				do {
					// sprawdzam odleglosc i powiadamiam jak trzeba
					if (curLocation.distanceTo(destLocation) < myAlertRadiusValue) {
						// powiadomienie
						// toast
						Toast.makeText(getBaseContext(),
								"UWAGA ZBLIZASZ SIE DO RADARU !",
								Toast.LENGTH_LONG).show();

						// taram !!!
						try {
							if ((mMediaPlayer == null)
									|| (!mMediaPlayer.isPlaying())) {
								mMediaPlayer = MediaPlayer.create(
										getBaseContext(), R.raw.gong);
								mMediaPlayer.start();
								Log.i("MediaPlayer",
										"Powiadomiono o radarze OK");
							}
						} catch (Exception e) {
							Log.e("MediaPlayer", "error: " + e.getMessage(), e);
						}

					}

					i++;
				} while (poiCursor.moveToNext());

		}
	};

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

}
